<template>
  <div class="app-container">
    <div class="filter">
      <el-button icon="el-icon-plus" type="primary" @click="handleCreate">添加</el-button>
    </div>
    <el-table
      ref="adminTable"
      v-loading="loading"
      :data="list"
      row-key="id"
      :stripe="true"
      border
      highlight-current-row
      style="width: 100%"
      max-height="750"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="ID" prop="id" width="120">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图标" prop="icon" align="center" width="80">
        <template slot-scope="{row}">
          <span v-if="row.icon"><svg-icon :icon-class="row.icon" /></span>
        </template>
      </el-table-column>
      <el-table-column label="权限名" prop="name" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="菜单名" prop="title" align="center">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址" prop="path" align="center">
        <template slot-scope="{row}">
          <span>{{ row.path }}</span>
        </template>
      </el-table-column>
      <el-table-column label="组件路径" prop="component" align="center">
        <template slot-scope="{row}">
          <span>{{ row.component }}</span>
        </template>
      </el-table-column>
      <el-table-column label="重定向" prop="redirect" align="center">
        <template slot-scope="{row}">
          <span>{{ row.redirect }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" prop="is_menu" align="center" width="80">
        <template slot-scope="{row}">
          <el-tag v-if="row.is_menu" type="success">菜单</el-tag>
          <el-tag v-else type="info">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="Hidden" prop="hidden" align="center" width="80">
        <template slot-scope="{row}">
          <el-tag v-if="row.hidden" type="danger">隐藏</el-tag>
          <el-tag v-else>显示</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" prop="action" align="center" fixed="right" width="200">
        <template slot-scope="{row}">
          <el-button type="text" icon="el-icon-plus" @click="handleAdd(row)">添加</el-button>
          <el-button type="text" icon="el-icon-edit" @click="handleEdit(row)">修改</el-button>
          <el-button type="text" style="color: red" icon="el-icon-delete" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialog" :title="dialogType==='edit'?'修改':'新增权限'" width="700px">
      <el-form ref="userForm" v-model="temp" :rules="rules" label-position="right" label-width="150px">
        <el-form-item label="父级权限" prop="pid">
          <el-cascader
            v-model="temp.pid"
            placeholder="一级菜单"
            :options="list"
            :props="props"
            :show-all-levels="false"
            clearable
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item v-if="temp.pid === 0 || !temp.pid" label="菜单图标" prop="icon">
          <el-popover
            placement="bottom-start"
            width="460"
            trigger="click"
          >
            <IconPicker ref="iconPicker" @selected="selected" @show="$refs['iconPicker'].reset()" />
            <el-input slot="reference" v-model="temp.icon" placeholder="点击选择图标" clearable style="width:300px">
              <svg-icon v-if="temp.icon" slot="prefix" :icon-class="temp.icon" class="el-input__icon" style="height: 40px;width: 16px;margin-left: 5px" />
              <i v-else slot="prefix" class="el-icon-search el-input__icon" />
            </el-input>
          </el-popover>
        </el-form-item>
        <el-form-item label="权限名" prop="name">
          <el-input v-model="temp.name" style="width:400px" />
        </el-form-item>
        <el-form-item label="菜单名" prop="title">
          <el-input v-model="temp.title" style="width:400px" />
        </el-form-item>
        <el-form-item label="地址" prop="path">
          <el-input v-model="temp.path" style="width:400px" />
        </el-form-item>
        <el-form-item v-if="temp.pid > 0" label="组件路径" prop="component">
          <el-input v-model="temp.component" style="width:400px" />
        </el-form-item>
        <el-form-item v-if="temp.pid === 0" label="重定向" prop="redirect">
          <el-input v-model="temp.redirect" style="width:400px" />
        </el-form-item>
        <el-form-item label="类型" prop="is_menu">
          <el-radio-group v-model="temp.is_menu">
            <el-radio :label="1">菜单</el-radio>
            <el-radio :label="0">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="Hidden" prop="hidden">
          <el-radio-group v-model="temp.hidden">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog=false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { permissionList, store, update, destroy } from '@/api/permission/permission'
import IconPicker from '@/components/IconPicker'

export default {
  name: 'PermissionPermission',
  components: { IconPicker },
  data() {
    return {
      total: 0,
      list: [],
      roleList: [],
      loading: true,
      dialog: false,
      dialogType: 'create',
      rules: {},
      temp: {
        pid: 0,
        is_menu: 1,
        hidden: 0
      },
      props: {
        value: 'id',
        label: 'title',
        children: 'children',
        checkStrictly: true,
        emitPath: false
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 图标选择器
    selected(name) {
      this.temp.icon = name
    },
    // 获取管理员列表
    getList() {
      this.loading = true
      permissionList().then(res => {
        this.list = res.data
        setTimeout(() => {
          this.loading = false
        }, 500)
      })
    },
    // 打开新增权限窗口
    handleCreate() {
      this.temp = {
        pid: 0,
        is_menu: 1,
        hidden: 0
      }
      this.selectId = 0
      this.dialogType = 'create'
      this.dialog = true
      this.$nextTick(() => {
        this.$refs['userForm'].clearValidate()
      })
    },
    // 打开新增子权限窗口
    handleAdd(row) {
      this.temp = {
        pid: row.id,
        is_menu: 1,
        hidden: 0,
        name: row.name + '-'
      }
      this.selectId = 0
      this.dialogType = 'create'
      this.dialog = true
      this.$nextTick(() => {
        this.$refs['userForm'].clearValidate()
      })
    },
    // 打开修改权限窗口
    handleEdit(row) {
      this.temp = Object.assign({}, row)
      this.selectId = row.pid
      this.dialogType = 'edit'
      this.dialog = true
      // 将默认的角色转换为一维数组
      this.$nextTick(() => {
        this.$refs['userForm'].clearValidate()
      })
    },
    // 提交管理员数据（新增或修改）
    save() {
      const tempDate = Object.assign({}, this.temp)
      if (!tempDate.is_menu) {
        tempDate.hidden = 1
      }
      // 一级菜单组件路径默认为'#'
      if (tempDate.pid === 0) {
        tempDate.component = '#'
      }
      const isEdit = this.dialogType === 'edit'
      if (isEdit) {
        update(tempDate, tempDate.id).then(res => {
          this.getList()
          this.dialog = false
          this.$notify({
            title: '修改权限菜单',
            message: res.message,
            type: 'success',
            duration: 2000
          })
        })
      } else {
        store(tempDate).then(res => {
          this.getList()
          this.dialog = false
          this.$notify({
            title: '添加权限菜单',
            message: res.message,
            type: 'success',
            duration: 2000
          })
        })
      }
    },
    // 删除
    del(id) {
      this.$confirm('删除该权将同时删除该权限子目录，是否确认该权限菜单？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await destroy(id)
        this.getList()
        this.$notify({
          title: '删除权限菜单',
          message: res.message,
          type: 'success',
          duration: 2000
        })
      })
    },
    // 监听下拉框值
    handleChange(value) {
      if (!value) {
        this.temp.pid = 0
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .app-container {
    .filter {
      margin-bottom: 20px;

      .filter-input {
        margin-right: 10px;
      }
    }
  }
</style>
